﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace memo
{
    /// <summary>
    /// Used for storing data permanently on the hard drive, on the path SavedFilePath.
    /// The Path is given a default in the constructor, but it is modifyable.
    /// IMPORTANT NOTE: the exceptions occured must be handled outside the class!
    /// </summary>
    public class DataAccessor : IDataAccessor
    {

        /// <summary>
        /// Stores the full path (with the filename) of the data file.
        /// </summary>
        public string SavedFilePath { get; set; }

        /// <summary>
        /// Creates a DataAccessor with a default file-path.
        /// </summary>
        public DataAccessor()
        {
            SavedFilePath = "Data.memo";
        }

        /// <summary>
        /// Saves the array of Topics to the file system in the path set in SavedFilePath.
        /// </summary>
        /// <param name="topics">The main topic list.</param>
        /// <exception cref="memo.MemoDataAccessorException">Throws when some problem occurs during the saving process. </exception>
        public void Save(List<Topic> topics)
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream(SavedFilePath, FileMode.Append);
                BinaryFormatter serializer = new BinaryFormatter();
                serializer.Serialize(fs, topics);
            }
            catch (Exception e)
            {
                // amit lehet iffel el kéne kerülni, leellenőrizzük
                throw new MemoDataAccessorException("Error during saving the topics into the file system.", e);
            }
            finally
            {
                fs.Close();
            }
        }

        /// <summary>
        /// Loads the array of Topics from the file system in the path set in SavedFilePath.
        /// </summary>
        /// <exception cref="memo.MemoDataAccessorException">Throws when some problem occurs during the loading process. </exception>
        public List<Topic> Load()
        {
            List<Topic> topicList;

            if (File.Exists(SavedFilePath) == true)
            {
                FileStream fs = null;
                try
                {
                    fs = new FileStream(SavedFilePath, FileMode.Open, FileAccess.Read);
                    BinaryFormatter serializer = new BinaryFormatter();
                    topicList = (List<Topic>)serializer.Deserialize(fs);
                }
                catch (Exception)
                {
                    throw new MemoDataAccessorException("Error during loading the topics form the file system.");
                }
                finally
                {
                    fs.Close();
                }
            }
            else
            {
                topicList = new List<Topic>();
                Save(topicList);
            }

            return topicList;
        }
    }
}
